package arithmetic;

import org.junit.jupiter.api.Test;

/**
 * 最后一个单词的长度
 *
 * @author Gaojie
 * @date 2022/3/13 17:41
 **/
public class LengthOfLastWord {

    @Test
    void test() {
        assert fun("Hello World") == 5;
        assert fun("  AA BB CCC ") == 3;
        assert fun("luffy is still joyboy") == 6;
        assert fun("   fly me   to   the moon  ") == 4;
    }

    /**
     * 1.去空格
     * 2.根据空格切割
     * 3.返回最后一个的长度
     */
    int fun(String s) {
        String[] words = s.trim().split(" ");
        return words[words.length - 1].length();
    }
}
